
$(document).ready(function(){
    // render
    loadData();
    // event
    bindEvent();
});

function loadData() {

    ISS.BACKEND.request({
        url: ISS.SESSION.getSession('contextPath') + '/service/admin/ad_GetProductComment',
        headers: {
            'Accept': 'application/json',
            'Content-Type': 'application/json'
        },
        type: 'GET',
        dataType: 'json',
        success: function (data) {
            var responseContent = data.responseObject;
            mapCommentToView(responseContent);
        }
    });
}

function bindEvent() {
    ISS.EVENT.bindEvent('#AdPost_Add', 'click', function () {
        if(validate()){
            sendInfo();
        }
        else {
            var htmlText = '<div class="box s-error mbs msgBox l-pageWrapper mbm">\
                                <div class="pam">Cập nhật bình luận thất bại</div>\
                            </div>';
            ISS.DOM.before('.l-register__benefits', htmlText);
        }
    });

    ISS.EVENT.bindEvent('#AdProductCommentCommon_Modified', 'click', function () {
        toEditPage();
    });
}

function sendInfo() {
    var post = {}, date;
    var category = {};

    post.maBV = ISS.DOM.getVal('#AdPost_Code');
    post.tieuDe = ISS.DOM.getVal('#AdPost_Title');
    post.tomTat = ISS.DOM.getVal('#AdPost_Summary');
//    date = ISS.DOM.getVal('#AdPost_Date');
//    var datemysql = date('Y-m-d',strtotime(date));
//    post.ngayViet = date;
    post.ngayViet = ISS.DOM.getVal('#AdPost_Date');
    post.noiDung = ISS.DOM.getVal('#AdPost_Content');
    post.hinhAnh = ISS.DOM.getVal('#AdPost_Picture');
    category.maDMBV = ISS.DOM.getVal('#AdPost_Category');
    post.danhMucBaiViet = category;

    ISS.BACKEND.request({
        url: '../../service/admin/header/adPost',
        headers: {
            'Accept': 'application/json',
            'Content-Type': 'application/json'
        },
        type: 'POST',
        dataType: 'json',
        data: JSON.stringify(post),
        success: function (data) {
            var responseContent = data.responseObject;
        }
    });
}

function toEditPage() {
    var _maBL = [];
    ISS.DOM.forEach('#form-AdProductCommentCommon input[type="checkbox"]:checked', function(){
        _maBL.push($(this).val());
    });

    if(_maBL.length == 1) {
        var _url = '/service/admin/toProductCommentPage/' + _maBL[0];
        ISS.WINDOW.location(_url);
    }
    else {
        alert('Bạn chưa chọn dòng nào hoặc đã chọn hơn 1 dòng');
    }
}

function clearValidate() {
    ISS.DOM.removeClass('#AdPost_Code', 'error');
    ISS.DOM.removeClass('#AdPost_Title', 'error');
}

function validate() {
    var result = true;
    return result;
}

function mapCommentToView(comment) {
    var rows = comment.length;
    var columns = 1;
    var str = '';
    str += '<table class="scroll align-center">';
    str += '<thead>';
    str += '   <tr>';
    str += '       <th class="columnSTT">STT</th>';
    str += '       <th class="columnStick">Tick</th>';
    str += '       <th class="columnRule">Tên và Mã sản phẩm</th>';
    str += '       <th class="columnRule">Họ tên</th>';
    str += '       <th class="columnRule">Email</th>';
    str += '       <th class="columnRule">Nội dung</th>';
    str += '   </tr>';
    str += '</thead>';
    str += '<tbody>';
    for(var i = 0; i < rows; i++){
        if (i%2==0)
        {
            str += '<tr class="column-Dark">';
        }
        else
        {
            str += '<tr class="column-Light">';
        }

        for (var j = 0; j < columns; j++){
            str += '<td class="columnSTT">' + i + '</td>';
            str += '<td class="columnStick"><input id="' + comment[i].maBL + '" type="checkbox" value="' + comment[i].maBL + '"/></td>';
            str += '<td class="columnRule" id="' + comment[i].sanPham.maSP + '">' + comment[i].sanPham.tenSP + ' - ' + comment[i].sanPham.maSP + '</td>';
            str += '<td class="columnRule">' + comment[i].ten + '</td>';
            str += '<td class="columnRule">' + comment[i].email + '</td>';
            str += '<td class="columnRule">' + comment[i].noiDung + '</td>';
        }
        str += '</tr>';
    }
    str +=   '</tbody>';
    str += '</table>';
    ISS.DOM.html('#AdProductComment_All', str);
}